<?php
/**
 * 用户个人设置
 * Date: 2015/9/9
 * Time: 13:14
 */

namespace AdminV3\Controller;
use Think\Controller;
class UserSetController extends CommonController
{
    public function index()
    {
        if(empty($_POST)){
            $this->assign('uid',session('id'));
            $this->assign('page',M('User')->getFieldById(session('id'),'page'));
            $this->display();
        }else{
            $data['id'] = trim(I('post.uid'));
            $data['old_pw'] = trim(I('post.old_pw'));
            $data['pw'] = trim(I('post.pw'));
            $data['repw'] = trim(I('post.repw'));
            if(!in_array('',$data)){
                $model = M('User');
                 $password = $model ->getFieldById($data['id'],'password');
                if( $password != md5($data['old_pw'])){
                    $this->error('旧密码不正确!');
                }
                if($data['pw'] == $data['repw']){
                    $re = $model->where('id='.$data['id'])->setField('password',md5($data['pw']));
                    if($re>0){
                        $this->redirect('Console/index');
                    }else{
                        $this->error('密码修改不成功!');
                    }
                }else{
                    $this->error('两次输入的密码不一致!');
                }
            }else{
                $this->error('数据不完整!');
            }
        }

    }

    public function changePage()
    {
        $id = I('post.id');
        $npage = (int)I('post.npage');
        if(isset($id) && isset($npage) && $id>=1){

            $npage>=500?$npage =500:$npage;

            $npage<=10?$npage =10 :$npage;

            $re = M('User')->where('id ='.$id)->setField('page',$npage);
            if($re>0){
                $this->redirect('UserSet/index');
            }else{
                $this->error('修改分页数失败！');
            }


        }else{
            $this->error('非法操作！');
        }
    }

}